/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package huffman;

import static huffman.BitUtil.BITS;
import java.io.IOException;
import java.io.InputStream;

/**
 *
 * @author Jose Carlos Palma
 */
public class BitInputStream {

    private InputStream in;
    private int buffer;
    private int pos;

    public BitInputStream(InputStream in) {
        this.in = in;
        this.buffer = BITS;
    }

    public int readBit() throws IOException {
        if (buffer == BITS) {
            buffer = in.read();
            if (buffer == -1) {
                return -1;
            }
            pos = 0;
        }
        return (buffer & (1 << pos++)) == 0 ? 0 : 1;
    }
}
